import time
from datetime import datetime

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class CommonDriver:
    def get_driver(self):
        desc = {
            'platformName': 'Android',
            'platformVersion': '7',
            'appPackage': 'com.dangdang.buy2',
            'appActivity': 'com.dangdang.buy2.StartupActivity',
            'noReset': False,
            'unicodeKeyboard': True,
            'udid': '127.0.0.1:21513',
        }

        command_executor_url = 'http://127.0.0.1:4723/wd/hub'

        driver = webdriver.Remote(command_executor=command_executor_url, desired_capabilities=desc)

        driver.implicitly_wait(10)

        # print("连接成功")

        return driver


if __name__ == '__main__':
    # 当脚本直接运行时，创建CommonDriver实例并调用get_driver方法以获取WebDriver
    # 当脚本直接运行时，创建CommonDriver实例并调用get_driver方法以获取WebDriver
    driver = CommonDriver().get_driver()
    # 点击温馨提示的确定
    driver.find_element(AppiumBy.XPATH, '//android.widget.TextView[@text="同"]').click()
    # 点击授权弹框的确定
    driver.find_element(AppiumBy.XPATH,
                        '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # 点击新人专享的关闭按钮
    driver.find_element(AppiumBy.XPATH,
                        '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/dialog_cancel_tv"]').click()
    # 点击输入框
    driver.find_element(AppiumBy.XPATH,
                        '//android.widget.ViewFlipper[@resource-id="com.dangdang.buy2:id/home_view_flipper"]').click()
